'''
@author devdoer@gmail.com
'''
import time,threading,socket
from anole import AnoleLog
from anole.utils.url import Url
from anole.utils import procque
from anole.utils import urlloader 


class SockUrlLoader:
#load urls through socket	
        def __init__(self,sock):
		self.sock=sock
		self.log=AnoleLog().log
	def UrlItem(self,entry,**keyargs):
	#factory method ,create a UrlItem object
		return _UrlItem(entry,**keyargs)
		
		
						
	def __recv_all(self,sock,total):
		datas=[]
		left=total
		while 1:
			data=sock.recv(left)
			size=len(data)
			if size==left or size==0:
				break
			left-=size
			datas.append(data)
		data=''.join(datas)
		if len(data)<total:
			raise socket.error('not enough data')
		else:	
			return ''.join(datas)

	def load_urls(self):
		msg='MORE_URLS'
		size=len(msg)
		try:
			head=struct.pack('i',size)
			msg=head+msg
			self.sock.sendall(msg)
			head=self.sock.recv(4)
			size=struct.unpack(bytes)[0]
			data=self.__recv_all(self.sock,size)
			urls=marshal.loads(data)
			return urls 
		except :
			self.sock.close()
			raise
class DiskUrlLoader:
	def __init__(self,dir):
		self.__dir=dir
	def UrlItem(self,entry,**keyargs):
		return urlloader.FormatFileLoader.UrlItem(entry,**keyargs)
	def load_urls(self):
		files=procque.retr_undone_files(self.__dir,'txt')
		if not files:
			return []
		while 1:	
			try:
				file=files.pop(0)
			except IndexError:
				return []
			print file
			try:
				 urls=urlloader.FormatFileLoader(file).load()
			except IOError:
				procque.one_file_done(self.__dir,file)
				continue
			else:
				procque.one_file_done(self.__dir,file)
				return urls

